String.prototype.strip= function() {
    return this.replace(/^\s+/, '').replace(/\s+$/, '');
};

Controller('errors',{
	'.status click': function(params){
		ErrorsController.remove_editing();
		if(params.element.firstChild.nodeType == 1) return;
		var val =  params.element.innerHTML.strip();
		var id = params.element.getAttribute('error');
		params.element.setAttribute('old_value', val);
		params.element.innerHTML =ErrorsController.select_tag('err', val, ['new', 'accepted', 'resolved','closed','delete'], {'error': id});
		ErrorsController.editing = params.element;
		params.event.kill();
		
	},
	'.status select click': function(params){
		params.event.kill();
	},
	'.status select change': function(params){
		var id = params.element.getAttribute('error');
		if( params.element.value)
			this.value = params.element.value;
		else
			this.value = params.element[params.element.selectedIndex].value;
		
		if(this.value == 'delete'){
			var result = confirm('Are you sure you want to delete this error?');
			if(!result){
				ErrorsController.editing.innerHTML = ErrorsController.editing.getAttribute('old_value');
				ErrorsController.editing = null;
				return;
			}
		}
		
		new Ajax.Request('/errors/update_status',{asynchronous:true, 
					evalScripts:true, 
					parameters:'id='+id+'&status='+this.value,
					onComplete: this.continue_to('status_updated') });
		this.el = params.element.parentNode;
		this.el.innerHTML = 'updating ...';
		params.event.kill();
	},
	status_updated: function(){
		this.el.innerHTML = this.value;
		var status = 0;
		if(this.value == 'new')
			status = 1;
		else if(this.value == 'accepted')
			status = 2;
		else if(this.value == 'resolved')
			status= 3;
		else if(this.value == 'closed')
			status = 4;
		else if(this.value == 'delete'){
			this.el.parentNode.parentNode.removeChild(this.el.parentNode);
			ErrorsController.editing = null;
			return;
		}
		
		this.el.setAttribute('sorttable_customkey',status);
		this.el.className = 'status '+this.value;
		ErrorsController.editing = null;
	}
});
Controller('main',{
	click: function(){
		ErrorsController.remove_editing();
	}
});

ErrorsController.remove_editing = function(){
	if(ErrorsController.editing)
			ErrorsController.editing.innerHTML = ErrorsController.editing.firstChild.value;
	ErrorsController.editing = null;
};

//stolen to make things lighter
ErrorsController.select_tag= function(name, value, choices, html_options) {     
    html_options = html_options || {};
    html_options.id  = html_options.id  || name;
	html_options.name = name;
    var txt = '';
    txt += "<select error='"+html_options.error+"'>";
    for(var i = 0; i < choices.length; i++)
    {
        var choice = choices[i];
        if(typeof choice == 'string') choice = {value: choice};
		if(!choice.text) choice.text = choice.value;
		if(!choice.value) choice.text = choice.text;
		
		var optionOptions = {value: choice.value};
        if(choice.value == value)
            optionOptions.selected ='selected';
        txt += '<option value="'+choice.value+'" '+(optionOptions.selected? 'selected="selected">' : '>')  +choice.text+'</option>';
    }
    txt += '</select>';
    return txt;
};